﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.Baseframework.Model;
using Honestysoft.BaseObject;
using System.Web.SessionState;
using System.IO;
using System.Configuration;
using Honestysoft.Financing.Model;

namespace Honestysoft.Financing.WebUI.Module.Attach.Json
{
    /// <summary>
    /// UploadProjectDocument 的摘要说明
    /// </summary>
    public class UploadProjectDocument : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string reqData=context.Request.Params["DataID"];
            string dataId = reqData.Split('|')[0];
            string buType = reqData.Split('|')[1];
            string uploadpath = "";// context.Request["folder"];
            if (buType == ((int)Honestysoft.Financing.Model.EnumItem.AttachmentBuType.ProjectDocument).ToString())
            {
                BD_Project project = new BaseBusinessLogic<BD_Project>().GetEntity(dataId);
                uploadpath = project.SN + project.Name + "/基础资料";
            }
            else
            {
                BD_Contract contract = new BaseBusinessLogic<BD_Contract>().GetEntity(dataId);
                BD_Project project = new BaseBusinessLogic<BD_Project>().GetEntity(contract.ProjectID);
                uploadpath = project.SN + project.Name + "/合同资料/" + project.SN + "-" + contract.Code.Substring(contract.Code.IndexOf("-") + 1);
            }
            uploadpath = "Document/" + uploadpath;
            try
            {
                HttpPostedFile file = context.Request.Files["FileData"];
                string extName = file.FileName.Substring(file.FileName.LastIndexOf(".") + 1);
                string fileName = file.FileName.Substring(0, file.FileName.LastIndexOf("."));
                if (!Directory.Exists(context.Server.MapPath("~/" + uploadpath)))
                {
                    Directory.CreateDirectory(context.Server.MapPath("~/" + uploadpath));
                }
                if (File.Exists(context.Server.MapPath("~/" + uploadpath + "/" + fileName + "." + extName)))
                {
                    File.Delete(context.Server.MapPath("~/" + uploadpath + "/" + fileName + "." + extName));
                }
                file.SaveAs(context.Server.MapPath("~/" + uploadpath + "/" + fileName + "." + extName));
                QueryParameters query = new QueryParameters();
                query.Add("DataID", dataId, QueryParameters.OperatorType.Equal);
                query.Add("AttachmentBuType", buType, QueryParameters.OperatorType.Equal);
                query.Add("FileName", fileName, QueryParameters.OperatorType.Equal);
                query.Add("Extension", extName, QueryParameters.OperatorType.Equal);
                BaseBusinessLogic<Sys_Attachment> bll = new BaseBusinessLogic<Sys_Attachment>(Globals.CurrentConnStringName);
                List<Sys_Attachment> lsAtt = bll.GetEntitys(query);
                Sys_Attachment obj = null;
                if (lsAtt == null || lsAtt.Count == 0)//没有附件
                {
                    obj = new Sys_Attachment();
                    obj.AttachmentBuType = buType;
                    obj.CreateTime = DateTime.Now;
                    obj.Creator = UserLoginer.LoginUser.ID;
                    obj.DataID = dataId;
                    obj.ID = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
                    obj.StoreType = ((int)Honestysoft.Baseframework.Model.EnumItem.StoreType.Disk).ToString();
                }
                else
                {
                    obj = lsAtt[0];
                }
                obj.Extension = extName;
                obj.Size = file.ContentLength;
                obj.FileName  =fileName;
                obj.Path = uploadpath;
                if (lsAtt == null || lsAtt.Count == 0)//没有附件
                {
                    bll.CreateEntity(obj);
                }
                else
                {
                    bll.UpdateEntity(obj);
                }
                context.Response.Write("1");
            }
            catch
            {
                context.Response.Write("0");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}